package cn.csc.module.product.service.tagref;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.product.controller.admin.tagref.vo.SpuTagReferenceCreateReqVO;
import cn.csc.module.product.controller.admin.tagref.vo.SpuTagReferenceExportReqVO;
import cn.csc.module.product.controller.admin.tagref.vo.SpuTagReferencePageReqVO;
import cn.csc.module.product.controller.admin.tagref.vo.SpuTagReferenceUpdateReqVO;
import cn.csc.module.product.convert.tagref.SpuTagReferenceConvert;
import cn.csc.module.product.dal.dataobject.tagref.SpuTagReferenceDO;
import cn.csc.module.product.dal.mysql.tagref.SpuTagReferenceMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.product.enums.ErrorCodeConstants.SPU_TAG_REFERENCE_NOT_EXISTS;

/**
 * 商品标签关联信息 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class SpuTagReferenceServiceImpl implements SpuTagReferenceService {

    @Resource
    private SpuTagReferenceMapper spuTagReferenceMapper;

    @Override
    public BigInteger createSpuTagReference(SpuTagReferenceCreateReqVO createReqVO) {
        // 插入
        SpuTagReferenceDO spuTagReference = SpuTagReferenceConvert.INSTANCE.convert(createReqVO);
        spuTagReferenceMapper.insert(spuTagReference);
        // 返回
        return spuTagReference.getId();
    }

    @Override
    public void updateSpuTagReference(SpuTagReferenceUpdateReqVO updateReqVO) {
        // 校验存在
        validateSpuTagReferenceExists(updateReqVO.getId());
        // 更新
        SpuTagReferenceDO updateObj = SpuTagReferenceConvert.INSTANCE.convert(updateReqVO);
        spuTagReferenceMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpuTagReference(BigInteger id) {
        // 校验存在
        validateSpuTagReferenceExists(id);
        // 删除
        spuTagReferenceMapper.deleteById(id);
    }

    private void validateSpuTagReferenceExists(BigInteger id) {
        if (spuTagReferenceMapper.selectById(id) == null) {
            throw exception(SPU_TAG_REFERENCE_NOT_EXISTS);
        }
    }

    @Override
    public SpuTagReferenceDO getSpuTagReference(BigInteger id) {
        return spuTagReferenceMapper.selectById(id);
    }

    @Override
    public List<SpuTagReferenceDO> getSpuTagReferenceList(Collection<BigInteger> ids) {
        return spuTagReferenceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SpuTagReferenceDO> getSpuTagReferencePage(SpuTagReferencePageReqVO pageReqVO) {
        return spuTagReferenceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SpuTagReferenceDO> getSpuTagReferenceList(SpuTagReferenceExportReqVO exportReqVO) {
        return spuTagReferenceMapper.selectList(exportReqVO);
    }

    /**
     * 根据spuId获取商品关联标签列表
     *
     * @param spuId 商品spu ID
     * @return
     */
    @Override
    public List<SpuTagReferenceDO> getBySpuId(BigInteger spuId) {
        return spuTagReferenceMapper.selectBySpuId(spuId);
    }
}
